﻿using System.Web.Mvc;
using System.Web.Routing;

namespace System.Web.Mvc
{
    public static class HtmlExtensions
    {
        public static MvcHtmlString SubMenuLink(this HtmlHelper helper,
               string linkText, string actionName,
               string controllerName, string className, string title)
        {
            string currentAction = helper.ViewContext.RouteData.GetRequiredString("action");
            string currentController = helper.ViewContext.RouteData.GetRequiredString("controller");

            var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
            var anchor = new TagBuilder("a");
            anchor.InnerHtml = linkText;
            anchor.Attributes["href"] = urlHelper.Action(actionName, controllerName);

            if (controllerName == currentController && actionName == currentAction)
            {
                object htmlAttributes = new { @class = "selected" };
                anchor.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            }

            if (!string.IsNullOrEmpty(className))
                anchor.Attributes["class"] = className;

            if (!string.IsNullOrEmpty(title))
                anchor.Attributes["title"] = title;

            return MvcHtmlString.Create(anchor.ToString());
        }

        public static MvcHtmlString SubMenuLink(this HtmlHelper helper,
               string linkText, string actionName,
               string controllerName)
        {
            return SubMenuLink(helper, linkText, actionName, controllerName, null, null);
        }
    }
}